package file

import (
	"fmt"
	"os"
)

const (
	B = 1 << (10 * iota)
	KiB
	MiB
	GiB
	TiB
	PiB
)

func FormatFileSize(fileSize int64) (size string) {
	switch {
	case fileSize < KiB:
		return fmt.Sprintf("%.2fB", float64(fileSize)/float64(B))
	case fileSize < MiB:
		return fmt.Sprintf("%.2fKB", float64(fileSize)/float64(KiB))
	case fileSize < GiB:
		return fmt.Sprintf("%.2fMB", float64(fileSize)/float64(MiB))
	case fileSize < TiB:
		return fmt.Sprintf("%.2fGB", float64(fileSize)/float64(GiB))
	case fileSize < PiB:
		return fmt.Sprintf("%.2fTB", float64(fileSize)/float64(TiB))
	default:
		return fmt.Sprintf("%.2fPB", float64(fileSize)/float64(PiB))
	}
}

func Exist(file string) bool {
	_, err := os.Stat(file)
	if os.IsNotExist(err) {
		return false
	}
	return true
}
